package com.gexin.model.product;

import com.gexin.model.product.base.BaseProductSkuKey;
import com.jfinal.kit.JsonKit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务名称:  t_product_sku_key(sku属性键表)
 */
@SuppressWarnings("serial")
public class ProductSkuKey extends BaseProductSkuKey<ProductSkuKey> {
	public static final ProductSkuKey dao = new ProductSkuKey();
	/**
	 * 查询系统定义好的key
	 * @return
	 */
	public List<ProductSkuKey> getMainKey(){
		return dao.find("SELECT key_id,key_name from t_product_sku_key where key_mode=1");
	}
	/**
	 * 根据店铺查询定义好的sku   包括了 系统定义好的sku
	 * @param shopid  店铺Id
	 * @return
	 */
	public List<ProductSkuKey>  getKeyByShopId(String shopid){
		return dao.find("SELECT key_id,key_name from t_product_sku_key where key_mode=1 or creator_shopid=?",shopid);
	}

	/**
	 * 根据店铺查询定义好的sku   包括了 系统定义好的sku  转换为JSON
	 * @param shopid  店铺Id
	 * @return
	 */
	public String  getKeyByShopId_ToJson(String shopid){
		List<ProductSkuKey>  list =getKeyByShopId(shopid);
		Map<Integer, String> map =new HashMap<Integer, String>();
		for(ProductSkuKey key : list ){
			map.put(key.getKeyId(), key.getKeyName());
		}
		return  JsonKit.toJson(map);
	}

	/**
	 * 剔除  第一个norm1
	 * fang 2016-06-21
	 * @param shopid  店铺Id
	 * @return
	 */

	public List<ProductSkuKey>  norm2_getKeyByShopId(String shopid, String key_id){
		return dao.find("SELECT key_id,key_name from t_product_sku_key where (key_mode=1 or creator_shopid=?) and key_id!= ?",shopid,key_id);
	}
	public String  norm2_getKeyByShopId_ToJson(String shopid,String key_id){
		List<ProductSkuKey>  list =norm2_getKeyByShopId(shopid,key_id);
		Map<Integer, String> map =new HashMap<Integer, String>();
		for(ProductSkuKey key : list ){
			map.put(key.getKeyId(), key.getKeyName());
		}
		return  JsonKit.toJson(map);
	}






	/**
	 * 剔除  第2个norm2
	 * @param shopid  店铺Id
	 * @return
	 */
	public List<ProductSkuKey>  norm3_getKeyByShopId(String shopid, String key_id, String key2_id){
		return dao.find("SELECT key_id,key_name from t_product_sku_key where (key_mode=1 or creator_shopid=?) and key_id not in(?,?)",shopid,key_id,key2_id);
	}

	public String  norm3_getKeyByShopId_ToJson(String shopid,String key_id,String key2_id){
		List<ProductSkuKey>  list =norm3_getKeyByShopId(shopid,key_id,key2_id);
		Map<Integer, String> map =new HashMap<Integer, String>();
		for(ProductSkuKey key : list ){
			map.put(key.getKeyId(), key.getKeyName());
		}
		return  JsonKit.toJson(map);
	}




	/**
	 * fang 2016-06-15
	 *   增加的时候     根据定义好的sku  拿到所有的 value
	 * 根据shop_id
	 */
	public String  getValueByShopId_ToJson(String shopid){
		List<ProductSkuKey>  list =getKeyByShopId(shopid);
		/*java.util.Map<Integer, Map<Integer, String>> map =new HashMap<Integer, Map<Integer, String>>();*/
		Map<Integer, String> map =new HashMap<Integer, String>();
		for(ProductSkuKey key : list ){
			/*List<ProductSkuValue>  list_value =ProductSkuValue.dao.getValueByKeyId(key.getKeyId());
			java.util.Map<Integer, String> map_value =new HashMap<Integer, String>();
			for(ProductSkuValue  key2 : list_value ){
				map_value.put(key2.getValueId(), key2.getValueName());
			}
			map.put(key.getKeyId(),map_value);*/
			map.put(key.getKeyId(),"");
		}
		return  JsonKit.toJson(map);
	}

	public String  edit_getValueByProductId_ToJson(String product_id,String shopid){
		List<ProductSkuKey>  list =getKeyByShopId(shopid);
		Map<Integer, Map<Integer, String>> map =new HashMap<Integer, Map<Integer, String>>();
		//java.util.Map<Integer, String> map =new HashMap<Integer, String>();
		for(ProductSkuKey key : list ){
			//List<ProductSkuValue>  list_value =ProductSkuValue.dao.getValueByKeyId(Integer.parseInt(key.getKeyId()));
			/*List<ProductSkuValue>  list_value =ProductSkuValue.dao.getKeyIdByProductIdAndKeyId(product_id+"", Integer.parseInt(key.getKeyId()));
			java.util.Map<Integer, String> map_value =new HashMap<Integer, String>();
			for(ProductSkuValue  key2 : list_value ){
				map_value.put(key2.getValueId(), key2.getValueName());
			}
			map.put(Integer.parseInt(key.getKeyId()),map_value);*/
			List<ProductSkuValue>  list_value = ProductSkuValue.dao.getKeyIdByProductIdAndKeyId(product_id, key.getKeyId());

			if(list_value.size()==0){
				map.put(key.getKeyId(),new HashMap<Integer, String>());
			}
			else{
				Map<Integer, String> map_value =new HashMap<Integer, String>();
				for(ProductSkuValue key2 : list_value ){
					map_value.put(key2.getValueId(), key2.getValueName());
				}
				map.put(key.getKeyId(),map_value);
			}
		}
		return  JsonKit.toJson(map);
	}

}
